package com.kly.user.controller.greetings;

import com.alibaba.excel.util.StringUtils;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.exception.common.ApplicationException;
import com.google.common.base.Splitter;
import com.kly.user.chatterService.GreetingService;
import com.kly.user.dto.chatter.GetModelGreetingReq;
import com.kly.user.dto.chatter.response.UserGreetingContentDto;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * model相关接口类
 * @author linqu
 */
@RestController
@RequestMapping("/api/greeting")
public class GreetingController {

    @Resource
    private GreetingService greetingService;



    /**
     * 对model打招呼
     */
    @ResponseBody
    @RequestMapping(value = "/getModelGreeting")
    public Response getModelGreeting(@RequestBody GetModelGreetingReq getModelGreetingReq) {
        if (StringUtils.isBlank(getModelGreetingReq.getChatterUserCode()) && StringUtils.isBlank(getModelGreetingReq.getModelUserCodes())) {
            throw new ApplicationException("chatterUserCode和modelUserCodes不能同时为空");
        }
        if (StringUtils.isNotBlank(getModelGreetingReq.getChatterUserCode()) && StringUtils.isBlank(getModelGreetingReq.getModelUserCodes())) {
            return greetingService.queryByChatterUserCode(getModelGreetingReq.getChatterUserCode());
        } else if (StringUtils.isBlank(getModelGreetingReq.getChatterUserCode()) && StringUtils.isNotBlank(getModelGreetingReq.getModelUserCodes())) {
            final String modelUserCodes = getModelGreetingReq.getModelUserCodes();
            final List<String> userCodes = Splitter.on(",").splitToList(modelUserCodes);
            return greetingService.queryByModelUserCodes(userCodes);
        } else if (StringUtils.isNotBlank(getModelGreetingReq.getChatterUserCode()) && StringUtils.isNotBlank(getModelGreetingReq.getModelUserCodes())) {
            final List<UserGreetingContentDto> userGreetingContentEntities = greetingService.queryByChatterUserCode(getModelGreetingReq.getChatterUserCode()).getData();
            if (CollectionUtils.isNotEmpty(userGreetingContentEntities)) {

                final String modelUserCodes = getModelGreetingReq.getModelUserCodes();
                final List<UserGreetingContentDto> filterModelGreetings = userGreetingContentEntities.stream().filter(o -> modelUserCodes.contains(o.getModelUserCode())).collect(Collectors.toList());
                return Response.ok(filterModelGreetings);
            }
        } return Response.ok();
    }
}
